function final_recount() {
	if (document.wheels_count == 0 && document.items_count == 0 && document.vinyls_count == 0) {
		get('sell_recount').innerHTML = "Ninguno seleccionado";
	} else {
		res = '<table class="table_sale_items">';
		res += '<tr><th>Descripcion</th><th>Cantidad</th><tr>';
		articles_total = 0;
		if (document.articles_count != 0) {
			for (i = 0; i < document.articles_count; i++) {
				articles_total += document.articles[i][0].fields.sale_price * document.articles[i][2].fields.multiplier;
			}
			res += '<tr><td>Repuestos</td><td>' + Math.round(articles_total*100)/100 + ' Bs</td></tr>';
		}
		vinyls_total = 0;
		if (document.vinyls_count != 0) {
			for (i = 0; i < document.vinyls_count; i++) {
				vinyls_total += get('vinyl_quantity_' + i).value * document.vinyls[i][0].fields.price * document.vinyls[i][1].fields.multiplier;
			}
			res += '<tr><td>Vinilos</td><td>' + Math.round(vinyls_total*100)/100 + ' Bs</td></tr>';
		}		
		wheels_total = 0;
		if (document.wheels_count != 0) {
			for (i = 0; i < document.wheels_count; i++) {
				wheels_total += get('wheel_price_' + i).value * document.wheels[i][get('wheel_currency_' + i).value].fields.multiplier;
			}
			res += '<tr><td>Ruedas</td><td>' + Math.round(wheels_total*100)/100 + ' Bs</td></tr>';
		}
		discount = document.discount * document.currencies[document.discount_currency-1].fields.multiplier;
		if (discount > 0) {
			res += '<tr><td>Descuento</td><td>' + Math.round(discount*100)/100 + ' Bs</td>';
		}
		res += '<tr><th>TOTAL</th><th><b>'+ Math.round((articles_total + vinyls_total + wheels_total - discount)*100)/100 +' Bs</b></th></tr>';
		res += '</table>';
		get('sell_recount').innerHTML = res;
	}
}

function vinyl_calculate(index) {
	quantity = get('vinyl_quantity_' + index);
	total = get('vinyl_total_price_' + index);
	total.innerHTML = Math.round(quantity.value * document.vinyls[index][1].fields.multiplier * document.vinyls[index][0].fields.price*100)/100;
	final_recount();
}

function wheel_calculate(index) {
	price = get('wheel_price_' + index);
	total = get('wheel_total_price_' + index);
	currency = get('wheel_currency_' + index);
	if (currency.value < 1 || currency.value > 3)
		currency.value = 1;
	total.innerHTML = Math.round(price.value * document.wheels[index][currency.value].fields.multiplier*100)/100;
	final_recount();
}

function recount_calculate() {
	items = get('articles');
	res = '<input type="hidden" name="articles_count" value="' + document.articles_count + '">';
	if (document.articles_count > 0) {
		res += '<div>Repuestos</div>';
		res += '<table class="table_sale_items">';
		res += '<tr><th>Nombre</th><th>Serial</th><th>Cantidad</th><th>Costo</th><th>Total</th></tr>';
		for (i = 0; i < document.articles.length; i++) {
			res += '<tr>';
			res += '<td><input type="hidden" name="article_' + i + '" value="'+document.articles[i][0].pk+'">' + document.articles[i][1].fields.name + '</td>';
			res += '<td>' + document.articles[i][0].fields.serial + '</td>';
			res += '<td>1</td>';
			res += '<td>' + document.articles[i][0].fields.sale_price + ' ' + document.articles[i][2].fields.symbol + '</td>';
			res += '<td>' + Math.round(document.articles[i][0].fields.sale_price * document.articles[i][2].fields.multiplier*100)/100 + ' Bs</td>';
			res += '</tr>';
		}
		res += '</table>';
	}
	items.innerHTML = res;
	
	vinyls = get('vinyls');
	res = '<input type="hidden" name="vinyls_count" value="' + document.vinyls_count + '">';
	if (document.vinyls_count > 0) {
		res += '<div>Vinilos</div>';
		res += '<table class="table_sale_items">';
		res += '<tr><th>Nombre</th><th>Codigo</th><th>Tama\u00f1o</th><th>Cantidad</th><th>Costo</th><th>Total</th></tr>';
		for (i = 0; i < document.vinyls.length; i++) {
			res += '<tr>';
			res += '<td><input type="hidden" name="vinyl_' + i + '" value="' + document.vinyls[i][0].pk + '">' + document.vinyls[i][0].fields.name + '</td>';
			res += '<td>' + document.vinyls[i][0].fields.code + '</td>';
			res += '<td>' + document.vinyls[i][0].fields.size + '</td>';
			res += '<td><input type="text" id="vinyl_quantity_' + i + '" name="vinyl_quantity_' + i + '" value="0" onkeyup="vinyl_calculate(' + i + ');"></td>';
			res += '<td>' + document.vinyls[i][0].fields.price + ' ' + document.vinyls[i][1].fields.symbol + '</td>';
			res += '<td><span id="vinyl_total_price_' + i + '">0</span> Bs</td>';
			res += '</tr>';
		}
		res += '</table>';
	}
	vinyls.innerHTML = res;

	wheels = get('wheels');
	res = '<input type="hidden" name="wheels_count" value="' + document.wheels_count + '">';
	if (document.wheels_count > 0) {
		res += '<div>Ruedas</div>';
		res += '<table class="table_sale_items">';
		res += '<tr><th>Tama\u00f1o</th><th>Marca</th><th>Codigo</th><th>Cantidad</th><th>Precio</th><th>Total</th></tr>';
		for (i = 0; i < document.wheels.length; i++) {
			res += '<tr>';
			res += '<td>' + document.wheels[i][0].fields.size + '</td>';
			res += '<td><input type="text" name="wheel_trade_mark_' + i + '"></td>';
			res += '<td><input type="text" name="wheel_code_' + i + '"></td>';
			res += '<td>1<input type="hidden" name="wheel_quantity_' + i + '" value="1"></td>';
			res += '<td><input type="text" id="wheel_price_' + i + '" name="wheel_price_' + i + '" onkeyup="wheel_calculate(' + i + ');">';
			res += '<select id="wheel_currency_' + i + '" name="wheel_currency_' + i + '" onchange="wheel_calculate(' + i + ')">';
				for (j = 1; j < document.wheels[i].length; j++) {
					res += '<option value="' + document.wheels[i][j].pk + '">' + document.wheels[i][j].fields.symbol + '</option>';
				}
			res += '</select></td>';
			res += '<td><span id="wheel_total_price_' + i + '">0</span> Bs</td>';
			res += '<input type="hidden" name="wheel_' + i + '" value="' + document.wheels[i][0].pk + '">';
			res += '</tr>';
		}
		res += '</table>';
	}
	wheels.innerHTML = res;
	final_recount();
}

function item_added() {
	data = document.request;
	for (i = 0; i < document.articles.length; i++) {
		if (data[0].pk == document.articles[i][0].pk) {
			alert('Este item ya esta en la lista de venta');
			return;
		}
	}
	document.articles[document.articles_count] = data;
	document.articles_count += 1;
	recount_calculate();
}

function vinyl_added() {
	data = document.request;
	for (i = 0; i < document.vinyls.length; i++) {
		if (data[0].pk == document.vinyls[i][0].pk) {
			alert('Este vinyl ya ha sido seleccionado');
			return;
		}
	}
	document.vinyls[document.vinyls_count] = data;
	document.vinyls_count += 1;
	recount_calculate();
}

function wheel_added() {
	data = document.request;
	document.wheels[document.wheels_count] = data;
	document.wheels_count += 1;
	recount_calculate();
}

function add_item(id) {
	ajax(ITEM_GET + id, 'item_added');
}

function add_vinyl(id) {
	ajax(VINYL_GET + id, 'vinyl_added');
}

function add_wheel(id) {
	ajax(WHEEL_GET + id, 'wheel_added');
}

function found_items() {
	items = document.request;
	res = '';
	if (items.length == 0) {
		res = '<b>No se encontraron resultados</b>';
	} else {
		res = '<b>Repuestos encontrados</b><p>';
		for (i = 0; i < items.length; i++) {
			res += '<a href="javascript:add_item(\''+items[i].pk+'\');">' + items[i].fields.serial + '</a><br>';
		}
		res += '<p>';
	}
	get('result_search').innerHTML = res;
}

function found_item_templates() {
	templates = document.request;
	res = '';
	if (templates.length == 0) {
		res = '<b>No se encontraron resultados</b>';
	} else {
		res = '<b>Items encontrados</b><p>';
		for (i = 0; i < templates.length; i++) {
			res += '<a href="javascript:load_template(' + templates[i].pk + ', \''+templates[i].fields.name+'\');">' + templates[i].fields.name + '</a> - '+templates[i].fields.detail+'<br>';
		}
		res += '<p>';
	}
	get('result_search').innerHTML = res;
}

function found_vinyls() {
	vinyls = document.request;
	res = '';
	if (vinyls.length == 0) {
		res += '<b>No se encontraron resultados</b>';
	} else {
		res = '<b>Vinilos encontrados</b><p>';
		for (i = 0; i < vinyls.length; i++) {
			res += '<a href="javascript:add_vinyl(' + vinyls[i].pk + ');">' + vinyls[i].fields.code + ' - ' + vinyls[i].fields.name + ' - Tamano ' + vinyls[i].fields.size + ' m - Cantidad ' + vinyls[i].fields.quantity + ' m</a><br>';
		}
		res += '<p>';
	}
	get('result_search').innerHTML = res;
}

function found_wheels() {
	wheels = document.request;
	res = '';
	if (wheels.length == 0) {
		res += '<b>No se encontraron resultados</b>';
	} else {
		res = '<b>Ruedas encontradas</b><p>';
		for (i = 0; i < wheels.length; i++) {
			res += '<a href="javascript:add_wheel(' + wheels[i].pk + ');">' + wheels[i].fields.size + '</a> - ' + wheels[i].fields.quantity + ' unidades<br>';
		}
		res += '<p>';
	}
	get('result_search').innerHTML = res;	
}

function loaded_template() {
	name = document.template_name;
	items = document.request;
	res = '';
	if (items.length == 0) {
		res = '<b>No se encontraron resultados</b>';
	} else {
		res = '<b>Items encontrados correspondientes a: "' + name + '"</b><p>';
		for (i = 0; i < items.length; i++) {
			res += '<a href="javascript:add_item(\'' + items[i].pk + '\');">' + items[i].fields.serial + ' (' + items[i].fields.warehouse + ')</a><br>';
		}
		res += '<p>';
	}
	get('result_search').innerHTML = res;
}

function load_template(id, name) {
	document.template_name=name;
	ajax(ITEM_LOAD + id, 'loaded_template');
}

function search_items() {
	if (get('text_search').value.length == 0) {
		alert('Ingrese algun texto de busqueda');
		get('text_search').focus();
		return;
	}
	url = ITEM_SEARCH;
	fun = 'found_items';
	if (get('type_search') != null) {
		if (get('type_search').value == 1) {
			url = ITEMTEMPLATE_SEARCH;
			fun = 'found_item_templates';
		}
		// Vinilos
		if (get('type_search').value == 3) {
			url = VINYL_SEARCH;
			fun = 'found_vinyls';
		}
		// Ruedas
		if (get('type_search').value == 4) {
			url = WHEEL_SEARCH;
			fun = 'found_wheels';
		}
		ajax(url + get('text_search').value, fun);
	}
}

function discount_change() {
	document.discount_currency = get('discount_currency').value;
	v = get('discount').value;
	if (isNaN(parseInt(v))) {
		document.discount = 0;
	} else {
		document.discount = v;
	}
	final_recount();
}

function onsubmit() {
	bill = get('bill_number');
	if (isNaN(parseInt(bill.value))) {
		alert('Debe ingresar el n\u00famero de la factura');
		bill.focus();
		return false;
	}
	if (document.articles_count == 0 && document.wheels_count == 0 && document.vinyls_count == 0) {
		alert('No se han ingresado \u00edtems de venta');
		return false;
	}
	return true;
}

function load_currencies() {
	document.currencies = document.request;
}

function start(){
	get('discount').onkeyup = discount_change;
	get('discount_currency').value = 1;
	get('btn_search').onclick = search_items;
	get('sale_form').onsubmit = onsubmit;
	get('discount_currency').onchange = discount_change;
	document.articles_count = 0;
	document.vinyls_count = 0;
	document.wheels_count = 0;
	document.articles = Array();
	document.vinyls = Array();
	document.wheels = Array();
	ajax(CURRENCIES_ALL, 'load_currencies');
}

window.onload = start;